﻿using System;
using System.Collections.Generic;
using ZyGames.DZPocker.Model;
using ZyGames.Framework.Cache.Generic;
using ZyGames.Framework.Game.Cache;
using ZyGames.Framework.Game.Context;
using ZyGames.Framework.Game.Contract;
using ZyGames.Framework.Game.Contract.Action;
using ZyGames.Framework.Game.Lang;
using ZyGames.Framework.Game.Service;
using System.Linq;
using ZyGames.DZPocker.Bll.Logic;
using ZyGames.DZPocker.Script;
using ZyGames.Framework.Net;
using ZyGames.DZPocker.Tools;

namespace ZyGames.DZPocker.Script.CsScript.Action
{
	/// <summary>
	/// 玩家的下注选择
	/// 看牌 弃牌 跟注 ...
	/// </summary>
	public class Action30007 : BaseAction
	{
		private int _tableId;
		private int _posId;
		private int _userId;
		/// <summary>
		/// 操作类型对应BetType(None = 0, Add, AllIn, Fold, Look, Follow)
		/// </summary>
		private int _betType;

		/// <summary>
		/// 下注筹码数量
		/// </summary>
		private int _chipNum;

		private bool _result = false;

		private TableData _currentTable;
		private GameUser _currentGameUser;

		private bool isSureForgive = false;
		private bool isShowRemindTips = false;
		private bool isForceNotRemind = false;
		public Action30007(HttpGet actionGetter)
			: base((short)ActionType.DZPlayBetChoose, actionGetter)
		{
		}
		public override bool GetUrlElement()
		{
			if (httpGet.GetInt("TableID", ref _tableId) &&
				httpGet.GetInt("UserID", ref _userId) &&
				httpGet.GetInt("PosID", ref _posId) &&
				httpGet.GetInt("BeatType", ref _betType) &&
				httpGet.GetInt("Chip", ref _chipNum) &&
				httpGet.GetBool("IsSureForgive", ref isSureForgive) &&
				httpGet.GetBool("IsForceNotRemind", ref isForceNotRemind))
			{
				return true;
			}
			return false;
		}

		public override bool TakeAction()
		{
			//TODO:根据选择判断是加注、跟注(30008消息)、弃牌还是AllIn(这两种状态广播20016消息)
			//TODO:玩家发送过来的筹码要进行校验是否合法,是否满足当前玩家所有的筹码,如果正好相等，则视为AllIn
			_currentTable = GameRoom.Current.GetTableData(_tableId);
			if (_currentTable == null)
			{
				Tool.WriteWarmingLine("消息300007获得房间不存在");
				return false;
			}
			if (_posId > _currentTable.PlayerNum)
			{
				Tool.WriteWarmingLine("消息30007座位号不合法");
				return false;
			}
			var pos = _currentTable.Positions[_posId - 1];
			if (pos == null)
			{
				Tool.WriteWarmingLine("消息30007座位号不存在");
				return false;
			}
			_currentGameUser = GameTable.Current.GetUser(_userId);
			if (_currentGameUser == null)
			{
				Tool.WriteWarmingLine("消息30007玩家不存在");
				return false;
			}
			if (isForceNotRemind && !_currentTable.ForceFoldUserIdList.Contains(UserId))
			{
				_currentTable.ForceFoldUserIdList.Add(UserId);
			}

			//判断某个整数是在定义在某个枚举类型中
			var res = Enum.IsDefined(typeof(PositionPlayState), _betType);

			if (!isSureForgive && (PositionPlayState)_betType == PositionPlayState.Fold && _currentTable.CurrentMaxChip == 0)
			{
				//如果当前这个桌子里面没有记录这个玩家则弹出弹框
				if (!_currentTable.ForceFoldUserIdList.Contains(UserId))
					this.isShowRemindTips = true;
			}
			if (!this.isShowRemindTips)
			{
				if (!res)
				{
					Tool.WriteWarmingLine("消息30007中 beatType不存在与PositionPlayState中");
					return false;
				}
				else
				{
					var beatType = (PositionPlayState)_betType;
					switch (beatType)
					{
						//目前貌似没有必要这样switch case这样写，暂时不改了
						case PositionPlayState.Fold:  //要判断游戏是否结束
						case PositionPlayState.AllIn:
						case PositionPlayState.Add:
						case PositionPlayState.Follow:
						case PositionPlayState.Look:
							{
								if (_chipNum > pos.Chip)
								{
									Tool.WriteWarmingLine(string.Format("桌子{0} 座位{1} 玩家ID{2} 存在作弊行为, 下注超出当前应有的筹码", _tableId, _posId, _userId));
									return false;
								}
								pos.PlayState = beatType;
								if (beatType == PositionPlayState.AllIn)
									pos.Chip = 0;
								else
									pos.Chip -= _chipNum;

								GameTable.Current.OnUserBet(_currentTable, _tableId, _chipNum, pos);
							}
							break;
						default:
							Tool.WriteWarmingLine("消息30007中玩家选择出现其他情况");
							break;
					}
					if (beatType == PositionPlayState.Fold)
					{
						GameTable.Current.OnUserGiveUp(_currentTable, _posId);
					}
					else
					{
						pos.BetState = false;
						//暂停定时器
						_currentTable.StopTimer();
						//继续下一个玩家
						GameTable.Current.NextBet(this._currentTable);
					}
				}
			}
			this._result = true;
			return true;
		}

		public override void BuildPacket()
		{
			this.PushIntoStack(_result);
			this.PushIntoStack(_tableId);
			this.PushIntoStack(isShowRemindTips);
		}
	}
}
